package leetcode;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * @Classname UndergroundSystem
 * @Author happytaohaha
 * @Date 2020/3/29
 * @Description 5370 设计地铁系统  需要考虑重复入地铁站的问题。
 */
public class UndergroundSystem {

    HashMap<Integer, Customer> customers = new HashMap<>();
    List<Customer> customerList = new ArrayList<>();

    private class Customer {
        int id;
        String startStation;
        String endStation;
        int startTime;
        int endTime;
        int costTime;
    }

    public UndergroundSystem() {

    }


    public void checkIn(int id, String stationName, int t) {
        Customer customer = new Customer();
        customer.id = id;
        customer.startStation = stationName;
        customer.startTime = t;
        customers.put(id, customer);
    }

    public void checkOut(int id, String stationName, int t) {
        Customer customer = customers.get(id);
        customer.endStation = stationName;
        customer.endTime = t;
        customer.costTime = customer.endTime - customer.startTime;
        customerList.add(customer);
    }

    public double getAverageTime(String startStation, String endStation) {
        double result = 0.0;
        int num = 0;
        for(Customer customer:customerList){
            if(customer.startStation.equals(startStation) && customer.endStation.equals(endStation)){
                result+= customer.costTime;
                num ++;
            }
        }
        return result / num * 1.0;
    }

    public static void main(String[] args) {
        UndergroundSystem undergroundSystem = new UndergroundSystem();
        undergroundSystem.checkIn(45, "Leyton", 3);
        undergroundSystem.checkIn(32, "Paradise", 8);
        undergroundSystem.checkIn(27, "Leyton", 10);
        undergroundSystem.checkOut(45, "Waterloo", 15);
        undergroundSystem.checkOut(27, "Waterloo", 20);
        undergroundSystem.checkOut(32, "Cambridge", 22);
        System.out.println(undergroundSystem.getAverageTime("Paradise", "Cambridge"));
        ;
        // 返回 14.0。从 "Paradise"（时刻 8）到 "Cambridge"(时刻 22)的行程只有一趟
        System.out.println(undergroundSystem.getAverageTime("Leyton", "Waterloo"));
        ;
        // 返回 11.0。总共有 2 躺从 "Leyton" 到 "Waterloo" 的行程，编号为 id=45 的乘客出发于 time=3 到达于 time=15，编号为 id=27 的乘客于 time=10 出发于 time=20 到达。所以平均时间为 ( (15-3) + (20-10) ) / 2 = 11.0
        undergroundSystem.checkIn(10, "Leyton", 24);
        System.out.println(undergroundSystem.getAverageTime("Leyton", "Waterloo"));
        ;
        // 返回 11.0
        undergroundSystem.checkOut(10, "Waterloo", 38);
        System.out.println(undergroundSystem.getAverageTime("Leyton", "Waterloo"));
        ;
        // 返回 12.0
    }
}
